package com.sdleyou.treepro.common.service.impl;

import com.sdleyou.treepro.common.model.dao.AdminDO;
import com.sdleyou.treepro.common.model.dao.AppointActiveDO;
import com.sdleyou.treepro.common.service.AdminManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service
public class AdminManagerImpl implements AdminManager {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean login(String username, String password) {
        StringBuilder sb = new StringBuilder(32);
        sb.append("username");
        sb.append("password");
        String md5Password = DigestUtils.md5DigestAsHex(sb.toString().getBytes());
        Query query = new Query(Criteria.where("username").is(username).and("password").is(md5Password));
        AdminDO adminDO = mongoTemplate.findOne(query, AdminDO.class);
        if (adminDO == null) {
            return false;
        }
        return true;
    }


    @Override
    public List<AppointActiveDO> appointmentList() {
        Query query = new Query(Criteria.where("active_id").ne(""));
        List<AppointActiveDO> activeDOList = mongoTemplate.find(query, AppointActiveDO.class);
        return activeDOList;
    }
}
